import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

/**
 * 选择WIFI弹窗
 */

class MyWifiDialog extends StatefulWidget {
  const MyWifiDialog(this.list, this.callback, {super.key});

  final List<Map<String, dynamic>> list;
  final Function callback;

  @override
  MyWifiDialogState createState() => MyWifiDialogState();
}

class MyWifiDialogState extends State<MyWifiDialog> {
  // int _selectedIndex = 0;

  _getContent() {
    return Container(
      height: 200,
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.all(10),
            child: Center(
                child: Visibility(
              visible: widget.list.isNotEmpty,
              child: Text(
                '选择WIFI',
                style: TextStyle(fontSize: 18),
              ),
            )),
          ),
          Expanded(
            child: ListView.separated(
                itemBuilder: (context, index) {
                  return InkWell(
                    onTap: () {
                      // print("点击了 $index");
                      // SmartDialog.dismiss(result: widget.list[index]);
                      widget.callback(index);
                    },
                    child: Container(
                      padding: const EdgeInsets.only(top: 10, bottom: 10),
                      child: Center(
                        child: Text(widget.list[index]["ssid"]),
                      ),
                    ),
                  );
                },
                separatorBuilder: (context, index) => Divider(
                      thickness: 1,
                      height: 0.5,
                      color: Colors.grey,
                    ),
                itemCount: widget.list.length),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _getContent();
  }
}

class _WifiWidget extends StatefulWidget {
  final List<Map<String, dynamic>> list;

  const _WifiWidget(this.list, {super.key});

  @override
  State<_WifiWidget> createState() => _WifiWidgetState();
}

class _WifiWidgetState extends State<_WifiWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
        width: 600,
        height: 260,
        decoration: BoxDecoration(
          borderRadius: const BorderRadius.all(Radius.circular(20.0)),
          color: Colors.white,
        ),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.all(10),
              child: Center(
                child: Text(
                  '请选择WIFI',
                  style: TextStyle(fontSize: 20),
                ),
              ),
            ),
            Expanded(
              child: ListView.separated(
                  itemBuilder: (context, index) {
                    return InkWell(
                      onTap: () {
                        print("点击了 $index");
                        SmartDialog.dismiss(result: widget.list[index]);
                      },
                      child: Container(
                        padding: const EdgeInsets.only(top: 10, bottom: 10),
                        child: Center(
                          child: Text(
                            widget.list[index]["ssid"],
                            style: TextStyle(fontSize: 16),
                          ),
                        ),
                      ),
                    );
                  },
                  separatorBuilder: (context, index) => Divider(
                        thickness: 1,
                        height: 1,
                        color: Colors.grey,
                      ),
                  itemCount: widget.list.length),
            )
          ],
        ));
  }
}
